package com.wink_172.utils

import android.content.Context
import android.os.Build
import android.provider.Settings
import android.util.Log
import com.wink_172.library.callback.ICallback
import com.wink_172.library.utils.*
import org.xutils.common.Callback
import org.xutils.http.RequestParams
import org.xutils.x
import java.util.LinkedHashMap


/**
 * 网络请求工具
 * */
object HttpRequest {

    private const val TAG = "HttpRequest"

    /**
     * 检查网络是否连接
     */
    fun checkNetWorkEnable(url:String,callback: ICallback) {
        val params =
           HttpRequest.getTargetParams(url)
//        params.addBodyParameter("post_id", post_id)
//        Log.e(TAG, "checkNetWorkEnable: ====>>" + params.toString())

        val commonCallback: Callback.CommonCallback<*> =
            object : Callback.CommonCallback<String> {
                override fun onSuccess(result: String) {
                    callback.onSendEvent(1)
                }

                override fun onError(ex: Throwable, isOnCallback: Boolean) {
                    callback.onSendEvent(2)

                    Log.e(TAG, "onError: ====>>"+ex.toString() )
                }

                override fun onCancelled(cex: Callback.CancelledException) {
                    callback.onSendEvent(3)
                }
                override fun onFinished() {
                    callback.onSendEvent(4)
                }
            }
        x.http().post(params, commonCallback)
    }


    /**
     * 获取携带签名的请求
     * */
    fun getTargetParams(url: String): RequestParams {
        val params = RequestParams(url)
        val timestamp = "" + System.currentTimeMillis() / 1000
        var signature = MD5Tools.stringToMD5(
            MD5Tools.stringToMD5(timestamp) + "@" + timestamp + "#" + MD5Tools.stringToMD5(
                timestamp
            )
        );

//        val app_version = AppUtils.getVersionName(x.app())
        params.addBodyParameter("timestamp", timestamp)
        params.addBodyParameter("signature", signature)
        params.addBodyParameter("phone_id", Build.DEVICE)

        params.addBodyParameter("app", "2")


        params.addHeader("host","gs.hongbaodada.com")
        params.addHeader("HOST","gs.hongbaodada.com")
        params.setHeader("Host","gs.hongbaodada.com")


//        Log.e(TAG, "hello02: ====>>"+params.headers[0].key+"   :"+ params.headers[0].value)
        return params;
    }


    /**
     *
     *PhoneUtils.getInfo()
     * {设备品牌名称：=HONOR, 设备名称：=HONORJSN-AL00a, 设备的型号：=JSN-AL00a, 设备制造商：=HUAWEI, 设备主板名称: =JSN-AL10, 设备硬件名称：=kirin710,
     * 设备驱动名称：=HWJSN-HM, Android ID：=4aee53a7b2586336,
     * 设备的唯一标识：(由设备的多个信息拼接合成)=HONOR/JSN-AL00a/HWJSN-HM:10/HONORJSN-AL00a/10.0.0.157C00:user/release-keys,
     * Android版本：=10, 设备版本：=10.0.0.157C00, 内核版本：=4.14.116}
     *
     * */
    fun getInfoTest(context: Context): Map<String, String>? {
        val infoMap: MutableMap<String, String> = LinkedHashMap()
        infoMap["设备品牌名称："] = Build.BRAND
        infoMap["设备名称："] = Build.ID
        infoMap["设备的型号："] = Build.MODEL
        infoMap["设备制造商："] = Build.MANUFACTURER
        infoMap["设备主板名称: "] = Build.BOARD
        infoMap["设备硬件名称："] = Build.HARDWARE
        infoMap["设备驱动名称："] = Build.DEVICE
        infoMap["Android ID："] =
            Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
//        infoMap["身份识别码MEID："] = PhoneUtils.getPhoneMEID(context)
//        infoMap["设备身份码IMEI："] = PhoneUtils.getPhoneIMEI(context)
//        infoMap["序列号: "] = PhoneUtils.getSerial()
//        infoMap["网口 MAC："] = PhoneUtils.getEth0MacAddress()
//        infoMap["WIFI MAC："] = PhoneUtils.getWiFiMacId(context)
        infoMap["设备的唯一标识：(由设备的多个信息拼接合成)"] = Build.FINGERPRINT
        infoMap["Android版本："] = Build.VERSION.RELEASE
        infoMap["设备版本："] = Build.VERSION.INCREMENTAL
        infoMap["内核版本："] = PhoneUtils.getKernelVersion()
        return infoMap

    }


}